package com.dunjx.user.controller;


import com.dunjx.user.pojo.RegionCity;
import com.dunjx.user.service.RegionCityService;
import entity.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dunjx.user.utils.RedisUtil;

import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "RegionCityController")
@RestController
@RequestMapping("/regionCity")
public class RegionCityController {


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RegionCityService regionCityService;

    @RequestMapping(value = "/searchByPid", method = RequestMethod.POST)
    public Result searchByPid(@RequestParam(value = "pid", required = false) Integer pid) {


        if (pid == null) {
            pid = 0;
        }

        String redisKeyPrefix = "region_";
        List<RegionCity> regionCities = null;
        if (null == redisUtil.get(redisKeyPrefix + pid)) {
            if (regionCityService.searchByPid(pid).size() != 0) {
                regionCities = regionCityService.searchByPid(pid);
                redisUtil.set(redisKeyPrefix + pid, regionCities, 1800L);
            }
        } else {
            regionCities = (List<RegionCity>) redisUtil.get(redisKeyPrefix + pid);
        }
        return new Result(true, StatusCode.OK, "查询城市列表成功", regionCities);
    }

}
